//
//  UIViewController+Function.m
//  KKCategory
//
//  Created by liaozhenming on 16/5/17.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "UIViewController+Function.h"

#import <objc/runtime.h>

@implementation UIViewController (Function)

+ (instancetype)kk_viewControllerFromXIB {
    
    return [self kk_viewControllerFromXIBWithNibName: NSStringFromClass(self.class)];
}

+ (instancetype)kk_viewControllerFromXIBWithNibName:(NSString *)nibName {
    
    if (nibName.length == 0) {
        nibName = NSStringFromClass(self.class);
    }
    
    UIViewController *viewController = [[super alloc] initWithNibName: nibName bundle: [NSBundle mainBundle]];
    return viewController;
}

+ (instancetype)kk_viewControllerFromStoryboard {
    return [self kk_viewControllerFromStoryboardWithIdentifier: NSStringFromClass(self.class)];
}

+ (instancetype)kk_viewControllerFromStoryboardWithIdentifier:(NSString *)identifier {
    return [self kk_viewControllerFromStoryboardWithStoryboardName: @"Main" identifier: identifier];
}

+ (instancetype)kk_viewControllerFromStoryboardWithStoryboardName:(NSString *)storyboardName identifier:(NSString *)identifier {
    
    if (storyboardName.length == 0) {
        storyboardName = @"Main";
    }
    if (identifier.length == 0) {
        identifier = NSStringFromClass(self.class);
    }
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName: storyboardName bundle: [NSBundle mainBundle]];
    
    UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier: identifier];
    
    return viewController;
}

#pragma mark - Setter methods
@dynamic kk_backEventHandle;

static const void *UIViewControllerBackEventHandleValue = &UIViewControllerBackEventHandleValue;

- (void)setKk_backEventHandle:(UIViewControllerBackEventHandle)kk_backEventHandle {
    objc_setAssociatedObject(self, UIViewControllerBackEventHandleValue, kk_backEventHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIViewControllerBackEventHandle)kk_backEventHandle {
    return objc_getAssociatedObject(self, UIViewControllerBackEventHandleValue);
}

@dynamic kk_tabbarHidden;

static const void *UIViewControllerTabbarHiddenValue = &UIViewControllerTabbarHiddenValue;

- (void)setKk_tabbarHidden:(BOOL)kk_tabbarHidden {
    objc_setAssociatedObject(self, UIViewControllerTabbarHiddenValue, @(kk_tabbarHidden), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)kk_tabbarHidden {
    return [objc_getAssociatedObject(self, UIViewControllerTabbarHiddenValue) boolValue];
}

#pragma mark - Public methods

- (void)kk_pushViewController:(UIViewController*)viewController {
    [self.navigationController pushViewController: viewController animated: YES];
}

- (void)kk_presentViewController:(UIViewController*)viewController {
    
    if (self.navigationController) {
        [self.navigationController presentViewController: viewController animated: true completion: nil];
    }
    else {
        [self presentViewController: viewController animated: true completion: nil];
    }
}

@end
